package com.yy.service.front.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.entity.front.AddressBook;
import com.yy.entity.front.User;
import com.yy.mapper.front.AddressBookMapper;
import com.yy.mapper.front.UserMapper;
import com.yy.service.front.AddressBookService;
import com.yy.service.front.UserService;
import com.yy.utils.BaseContext;
import com.yy.utils.MD5Utils;
import com.yy.utils.R;
import com.yy.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * create by KongFuning on 2022/8/4
 */
@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {


    /*
     * 添加地址信息
     * */
    @Override
    public R saveAddressBook(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        save(addressBook);
        return R.success(addressBook);
    }

    /*
     * 查询指定用户的全部地址
     * */
    @Override
    public R listAddressBook(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(addressBook.getUserId() != null,AddressBook::getUserId,addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        return R.success(list(queryWrapper));
    }

    /*
     * 设置默认地址
     * */
    @Override
    public R defaultAddress(AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        updateWrapper.set(AddressBook::getIsDefault,0);
        update(updateWrapper);

        addressBook.setIsDefault(1);
        updateById(addressBook);
        return R.success(addressBook);
    }

    /*
     * 编辑收货地址 回显数据
     * */
    @Override
    public R toUpdateAddress(String id) {
        AddressBook address = getById(id);
        return R.success(address);
    }

    /*
     * 保存收货地址
     * */
    @Override
    public R updateAddress(AddressBook addressBook) {
        addressBook.setUpdateTime(LocalDateTime.now());
        updateById(addressBook);
        return R.success(addressBook);
    }


    /*
     * 查询默认地址
     * */
    @Override
    public R getDefaultAddress() {
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        String userId = BaseContext.getCurrentId();
        qw.eq(userId != null,AddressBook::getUserId,userId);
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook address = getOne(qw);
        return R.success(address);
    }


}
